<?php
/**
 * CRMLite DbTable Class
 *
 * @final
 * @package Crmlite_App
 * @version $Id: Table.php 59 2007-09-10 03:35:38Z etng2004 $
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
abstract class Crmlite_Db_Table extends Zend_Db_Table
{

	/**
	 * 获得符合某条件的记录数
	 *
	 * @param array $where 筛选条件
	 * @return int
	 * @author 易波(Yi Bo) <etng2004@gmail.com>
	 */
	public function count($where = null)
	{
		// selection tool
		$select = $this->_db->select();
		// the FROM clause
		$select->from($this->_name, 'count(1) as cnt');
		// the WHERE clause
		settype($where, 'array');
		foreach ($where as $key => $val) {
			// is $key an int?
			if (is_int($key)) {
				// $val is the full condition
				$select->where($val);
			} else {
				// $key is the condition with placeholder,
				// and $val is quoted into the condition
				$select->where($key, $val);
			}
		}
		return intval($this->_db->fetchOne($select));
	}
	/**
	 * 根据某个字段进行分类汇总信息
	 *
	 * 主要用于比如按照状态显示tab时：未处理（10）、已处理（20）这样的数据汇总
	 * @see CampaignModel::statusStatics
	 * @param array $where			查询过滤条件
	 * @param string $group_field	汇总依据字段，暂时只支持一个字段
	 * @param array $default		基础数据，主要是汇总字段可能的出现的值为键值
	 * @return array				分类汇总信息
	 */
	function statics($where ,$group_field ,$default = array())
	{
		// selection tool
		$select = $this->_db->select();

		// the FROM clause
		$select->from($this->_name, $group_field . ',count(1) as cnt');

		// the WHERE clause
		$where = (array) $where;
		foreach ($where as $key => $val) {
			// is $key an int?
			if (is_int($key)) {
				// $val is the full condition
				$select->where($val);
			} else {
				// $key is the condition with placeholder,
				// and $val is quoted into the condition
				$select->where($key, $val);
			}
		}
		$select->group($group_field);
		$data= $this->_db->fetchPairs($select);
		foreach ($data as $key => $val)
		{
			$default[$key] = intval($val);
		}
		return $default;
	}
	/**
	 * 获得数据表的表名称
	 *
	 * @return string
	 * @author 易波(Yi Bo) <etng2004@gmail.com>
	 */
	public function getTable()
	{
		return $this->_name;
	}
	/**
	 * 获得数据表的主键名称
	 *
	 * @return string
	 * @author 易波(Yi Bo) <etng2004@gmail.com>
	 */
	function getPrimary()
	{
		return $this->_primary;
	}

	/**
	 * 一般用于判断Row|RowSet对象是否为null
	 *
	 * @param mixed $obj
	 * @return bool
	 */
	public function isNull( $obj )
	{
		if( $obj instanceof Zend_Db_Table_Row ){
			$array = $obj->toArray();
			$null = true;
			foreach( $array as $key => $value ){
				if( !empty( $value ) ){
					$null = false;
					break;
				}
			}
			return $null;
		}else if( $obj instanceof Zend_Db_Table_Rowset ){
			$array = $obj->toArray();
			return empty( $array ) ? true : false;
		}else if( is_array( $obj ) ){
			return count( $obj ) > 0 ? true : false;
		}else{
			return is_null( $obj ) ? true : false;
		}
	}
    /**
     * 获取数组库字段中的各字段默认值
     *
     * @return string
     */
	public function getDefaults()
	{
		$meta = $this->info();
		$ret = array();
		foreach ($meta['metadata'] as $field => $meta)
		{
			$ret[$field] = $meta['DEFAULT'];
		}
		return $ret;
	}
}
